<template lang="jade">
div
  h-loader-full(url='loader',:setdata='false')
  m-title(icon='add',name='添加培训课程')
  table.single
    mf-row(name='所属培训学校/机构')
      select(v-model='sch',placeholder='请选择培训学校/机构')
        option(value='', selected) 请选择所属学校/机构
        option(v-for='(s,i) in school', :value='s') {{s.name}}
    mf-row(name='培训课程显示位置')
      select(v-model='data.show',placeholder='请选择显示位置',v-if='school_show == 0')
        option(value='', selected) 请选择
        option(v-for='(n,i) in show', :value='i') {{n}}
      span(v-if='school_show != 0') {{show[data.show]}}
    mf-row(name='课程名称')
      input(type = 'text',v-model='data.name',placeholder='请填写课程名称')

    mf-row(name='是否免费')
      select(v-model='data.free',placeholder='是否免费')
        option(v-for='(s,i) in free_arr', :value='s.id') {{s.name}}


    mf-row(name='视频地址')
      input(type = 'text',v-model='data.vd_src',placeholder='请填写视频地址')
    mf-row(name='视频时长')
      input(type = 'text',v-model='data.vtime',placeholder='请填写视频时长')

    //- mf-row(name='创建时间：'): input(type='datetime-local',v-model='data.c_time')
    mf-row(name='观看人数')
      input(type = 'text',v-model='data.watch_num_input',placeholder='请填写观看人数') 
    mf-row(name='费用价格')
      input(type = 'text',v-model='data.money',placeholder='请填写费用价格') 
    mf-row(name='课程地点')
      input(type = 'text',v-model='data.address',placeholder='请填写课程地点') 
    mf-row(name='课程选择 | 间隔')
      input(type = 'text',v-model='data.select',placeholder='') 
    mf-row(name='图标图片')
      mf-upload(uploadid='img', amount='1', project='xiongan', category='logo', :data='data.icon',@upload_data='text',style='marginTop:20px')
      p(style='marginBottom:0px') 图标图片尺寸 80px（宽）*60px（高）
    mf-row(name='课程内容（公共描述）')
      textarea#con.editor(style='width: 500px; height: 200px;', v-model='data.content') {{data.content}}
    div(style = 'display:none')
      mf-row(name='培养目标（公共描述）')
        textarea#target.editor(style='width: 500px; height: 200px;', v-model='data.target') {{data.target}}
      mf-row(name='学制（公共描述）')
        textarea#system.editor(style='width: 500px; height: 200px;', v-model='data.system') {{data.system}}
      mf-row(name='就业方向')
      textarea#direction.editor(style='width: 500px; height: 200px;', v-model='data.direction') {{data.direction}}
      mf-row(name='是否在首页显示')
        select(v-model='data.ishome',placeholder='请选择')
          option(value='', selected) 请选择
          option(v-for='(n,i) in ishome', :value='i') {{n}}
    mf-row(name='是否启用')
      select(v-model='data.active',placeholder='请选择')
        option(value=1) 启用
        option(value=0) 禁用
    mf-row
      button(@click='button') 添加
</template>

<script>
export default {
	data () {
		return {
      loader:{
        school:'cultivate/school/list',
      },
      free_arr:[
        {
          id:0,
          name:"免费"
        },
        {
          id:1,
          name:"收费"
        }
      ],
      school: [],
      ishome: ['否','是'],
      show: ['两端显示','用户端','企业端'],
      school_show:'',
      sch:'',
      data:{
        sid:'',
        show:'',
        name:'',
        free:1,
        vd_src:'',
        // c_time:'',
        vtime:'',
        watch_num_input:'',
        money:'',
        address:'',
        select:'',
        icon:[],
        content:'',
        target:'',
        system:'',
        direction:'',
        ishome:'',
        number:'',
        active:1,
      },
		}
	},
  watch:{
    sch(newValue, oldValue){
      this.data.show = ''
      this.data.sid = newValue.id
      this.school_show = newValue.show
      if(this.school_show != 0){
        this.data.show = this.school_show
      }
    }
  },
	methods: {
    loadAfter(res){
      this.school = res.school[0]
		},
    text(data){
			this.data.icon = data
		},
    button() {
      con.sync()
      target.sync()
      system.sync()
      direction.sync()
      this.data.content = this.$dom.id('con').value
      this.data.target = this.$dom.id('target').value
      this.data.system = this.$dom.id('system').value
      this.data.direction = this.$dom.id('direction').value

      var init_num = 0;
      for(var key in this.data){
        if(this.data[key].toString() == ""){
          var must_str = "请填写";
          if(key == "sid" || key == "show"){
            must_str = "请选择";
          };
          if(key == "free" || key == "vd_src" || key=="watch_num_input" || key=="address" || key=="content" || key=="target" || key=="system" || key=="direction" || key=="ishome" || key=="number" || key=="money" || key=="select"){
            init_num++;
            continue;
          }
          this.$msg.error(must_str+$(".single").find("th").eq(init_num).text());
          return false;
        };
        init_num++;
      }



      // if(this.data.show !=0 && this.data.show !=1 && this.data.show !=2){
      //   this.$msg.error('请选择显示位置!')
      //   return
      // }
      // if(this.data.name == ''){
      //   this.$msg.error('请填写课程名称!')
      //   return
      // }
      // if(this.data.icon.length == 0 ){
      //   this.$msg.error('请上传图片!')
      //   return
      // }
      // if(this.data.sid == ''){
      //   this.$msg.error('请选择所属学校!')
      //   return
      // }
      // if(this.data.mobile == ''){
      //   this.$msg.error('请填写联系方式!')
      //   return
      // }
      // if(!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(this.data.mobile))){
      //   this.$msg.error('联系方式格式不正确！')
      //   return
      // }
      // if( this.data.content == ''){
      //   this.$msg.error('请填写课程内容!')
      //   return
      // }
      // if(this.data.target == ''){
      //   this.$msg.error('请填写培养目标!')
      //   return
      // }
      // if( this.data.system == ''){
      //   this.$msg.error('请填写学制!')
      //   return
      // }
      // if(this.data.direction == ''){
      //   this.$msg.error('请填写就业方向!')
      //   return
      // }
      // if(this.data.ishome != 0 && this.data.ishome != 1){
      //   this.$msg.error('请选择是否在首页显示!')
      //   return
      // }






      this.$ajax.post('cultivate/add/cultivate', this.data ,(res)=>{
        this.$msg.success(res.message)
        //console.log(this.data);
        //setTimeout("window.location.href='./cultivate_list.html'",1500)
      },(res)=>{
        this.$msg.error(res.message)
      })
    }
	}
}
</script>
